package cn.wikiflyer.beikang.act.user.setting;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.utils.WKAccessTokenKeeper;
import cn.wk.libs4a.utils.WKUmShareUtils;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;

public class UserSettingOAuthAct extends BaseActivity {

	@ViewInject(id = R.id.sina_oauth, click = "onClick")
	private Button sina;
	@ViewInject(id = R.id.tencent_oauth, click = "onClick")
	private Button qq;
	@ViewInject(id = R.id.weixin_oauth, click = "onClick")
	private Button weixin;

	@ViewInject(id = R.id.sina_o)
	private TextView sina_o;
	@ViewInject(id = R.id.tencent_o)
	private TextView qq_o;
	@ViewInject(id = R.id.weixin_o)
	private TextView weixin_o;

	private boolean sinaBool = false;// true--表示已授权；false--表示已登出授权
	private boolean qqBool = false;
	private boolean weixinBool = false;

	/**
	 * 友盟相关
	 */
	public WKUmShareUtils uMeng = null;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_user_setting_oauth);
	}

	@Override
	public void init() {
		uMeng = new WKUmShareUtils(ctx, app().weixinAppID,
				app().weixinAppSecret, app().tencentAppID, app().tencentAppKEY);
	}

	@Override
	public void setData() {
		// lazybone
		uMeng.setQZoneSDK(this);
		String sinaUid = WKAccessTokenKeeper
				.readAccessTokenUM(WKAccessTokenKeeper.SINAPREFERENCES_NAME,
						ctx).get(WKAccessTokenKeeper.KEY_ACCESS_TOKEN).toString();
		String tencentUid = WKAccessTokenKeeper
				.readAccessTokenUM(WKAccessTokenKeeper.QZONEPREFERENCES_NAME,
						ctx).get(WKAccessTokenKeeper.KEY_ACCESS_TOKEN).toString();
		String weixinUid = WKAccessTokenKeeper
				.readAccessTokenUM(WKAccessTokenKeeper.WEIXINPREFERENCES_NAME,
						ctx).get(WKAccessTokenKeeper.KEY_ACCESS_TOKEN_WEIXIN).toString();
		if (!TextUtils.isEmpty(sinaUid)) {
			setChecked(0);
		}
		if (!TextUtils.isEmpty(tencentUid)) {
			setChecked(1);
		}
		if (!TextUtils.isEmpty(weixinUid)) {
			setChecked(2);
		}
	}

	public void onClick(View v) {
		int id = v.getId();

		switch (id) {
		case R.id.tencent_oauth:
			if (qqBool)
				uMeng.logoutOAuth(SHARE_MEDIA.QZONE, R.id.tencent_oauth,
						handlerLogout, this);
			else
				uMeng.oAuthAction(SHARE_MEDIA.QZONE, R.id.tencent_oauth,
						handler, this);
			break;
		case R.id.weixin_oauth:
			if (weixinBool)
				uMeng.logoutOAuth(SHARE_MEDIA.WEIXIN, R.id.weixin_oauth,
						handlerLogout, this);
			else {
				UMWXHandler wxHandler = new UMWXHandler(ctx, uMeng.weixinAppID,
						uMeng.weixinAppSecret);
				wxHandler.addToSocialSDK();
				uMeng.oAuthAction(SHARE_MEDIA.WEIXIN, R.id.weixin_oauth,
						handler, this);
			}
			break;
		case R.id.sina_oauth:
			if (sinaBool)
				uMeng.logoutOAuth(SHARE_MEDIA.SINA, R.id.sina_oauth,
						handlerLogout, this);
			else {
				uMeng.mController.getConfig().setSsoHandler(
						new SinaSsoHandler());
				uMeng.oAuthAction(SHARE_MEDIA.SINA, R.id.sina_oauth, handler,
						this);
			}

			break;

		default:
			break;
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if ((Boolean) msg.obj) {
				String plat = "";
				int id = msg.what;
				switch (id) {
				case R.id.sina_oauth:
					uMeng.getPlatformInfo(SHARE_MEDIA.SINA);
					plat = "新浪微博";

					setChecked(0);
					break;
				case R.id.tencent_oauth:
					plat = "腾讯空间";
					uMeng.getPlatformInfo(SHARE_MEDIA.TENCENT);

					setChecked(1);
					break;
				case R.id.weixin_oauth:
					uMeng.getPlatformInfo(SHARE_MEDIA.WEIXIN);
					plat = "微信";

					setChecked(2);
					break;
				default:
					break;
				}
				WKApplication.showToast(plat + " 授权成功");
			}
		}
	};

	private Handler handlerLogout = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if ((Boolean) msg.obj) {
				String plat = "";
				int id = msg.what;
				switch (id) {
				case R.id.sina_oauth:
					plat = "新浪微博";
					WKAccessTokenKeeper.clear(
							WKAccessTokenKeeper.SINAPREFERENCES_NAME, ctx);

					setUnChecked(0);
					break;
				case R.id.tencent_oauth:
					plat = "腾讯空间";
					WKAccessTokenKeeper.clear(
							WKAccessTokenKeeper.QZONEPREFERENCES_NAME, ctx);

					setUnChecked(1);
					break;
				case R.id.weixin_oauth:
					plat = "微信";
					WKAccessTokenKeeper.clear(
							WKAccessTokenKeeper.WEIXINPREFERENCES_NAME, ctx);

					setUnChecked(2);
					break;

				default:
					break;
				}
				WKApplication.showToast("已取消 " + plat + " 授权");
			}
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		uMeng.activityResult(requestCode, resultCode, data);
	}

	// 设置已绑定
	private void setChecked(int id) {
		switch (id) {
		case 0:
			sinaBool = true;
			sina.setBackgroundResource(R.drawable.user_setting_checked);
			sina_o.setText("已绑定");
			sina_o.setTextColor(getResources().getColor(
					R.color.tab_btn_txt_selected));
			break;
		case 1:
			qqBool = true;
			qq.setBackgroundResource(R.drawable.user_setting_checked);
			qq_o.setText("已绑定");
			qq_o.setTextColor(getResources().getColor(
					R.color.tab_btn_txt_selected));
			break;
		case 2:
			weixinBool = true;
			weixin.setBackgroundResource(R.drawable.user_setting_checked);
			weixin_o.setText("已绑定");
			weixin_o.setTextColor(getResources().getColor(
					R.color.tab_btn_txt_selected));
			break;

		default:
			break;
		}
	}

	// 设置已绑定
	private void setUnChecked(int id) {
		switch (id) {
		case 0:
			sinaBool = false;
			sina.setBackgroundResource(R.drawable.user_setting_unchecked);
			sina_o.setText("未绑定");
			sina_o.setTextColor(getResources().getColor(R.color.bind));
			break;
		case 1:
			qqBool = false;
			qq.setBackgroundResource(R.drawable.user_setting_unchecked);
			qq_o.setText("未绑定");
			qq_o.setTextColor(getResources().getColor(R.color.bind));
			break;
		case 2:
			weixinBool = false;
			weixin.setBackgroundResource(R.drawable.user_setting_unchecked);
			weixin_o.setText("未绑定");
			weixin_o.setTextColor(getResources().getColor(R.color.bind));
			break;

		default:
			break;
		}
	}

}
